package com.stockz.ui.swing.charting.widget.toolbar;


import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;

import javax.swing.DefaultComboBoxModel;

import com.stockz.core.model.Scrip;
import com.stockz.ui.swing.charting.util.RefreshChartingDataUtil;
import com.stockz.ui.swing.charting.view.StockChartPanel;
import com.stockz.ui.swing.charting.widget.ScripNameComboBox;
import com.stockz.ui.swing.widget.FilteredComboBox.OnSelectionCallback;
import com.stockz.ui.swing.widget.JToolBar;

public class NavigationToolbar extends JToolBar{
	private static final long serialVersionUID = -4365778577203753210L;

	private final ScripNameComboBox scripNameComboBox;
	private final StockChartPanel stockChartPanel;
	private OnSelectionCallback onScripNameSelectionCallback = new OnSelectionCallback(){
		@Override
		public void onSelection(Object selectedItem) {
			if(stockChartPanel != null){
				stockChartPanel.getStockChartPanelState().setScrip((Scrip)selectedItem);
				RefreshChartingDataUtil.refreshChartingData(stockChartPanel);
			}
		}
	};
	@SuppressWarnings("unused")
	private ItemListener scripNameComboBoxItemListener =new ItemListener(){
		@Override
		public void itemStateChanged(ItemEvent e) {
			if(ItemEvent.SELECTED == e.getStateChange()){
				if(onScripNameSelectionCallback != null){
					onScripNameSelectionCallback.onSelection(e.getItem());
				}
			}
		}
	};
	
	public NavigationToolbar(StockChartPanel stockChartPanel, List<Scrip> scripList) {
		this.stockChartPanel = stockChartPanel;
		scripNameComboBox = new ScripNameComboBox();
		scripNameComboBox.setModel(new DefaultComboBoxModel(scripList.toArray()));
		scripNameComboBox.setOnSelectionCallback(onScripNameSelectionCallback);
		add(scripNameComboBox);
		//add(new ResetScripNameComboBoxButton(scripNameComboBox));
	}

}
